﻿using System;
using System.Web.Mvc;
using NBlog.Web.Application.Infrastructure;
using NBlog.Web.Application.Service;

namespace NBlog.Web.Controllers
{
    public partial class EntryController : LayoutController
    {
        public EntryController(IServices services) : base(services) { }

        [HttpGet]
        public ActionResult Show([Bind(Prefix = "id")] string slug)
        {
            if (string.IsNullOrWhiteSpace(slug))
                throw new ArgumentNullException("slug");

            if (Services.Page.Exists(slug))
            {
                var page = Services.Page.GetBySlug(slug);
                return View("Page", new PageModel(page));
            }
            
            if (Services.Article.Exists(slug))
            {
                var article = Services.Article.GetBySlug(slug);
                Services.Stats.AddView(slug);
                return View("Article", new ArticleModel(article));
            }

            if (Services.Review.Exists(slug))
            {
                var review = Services.Review.GetBySlug(slug);
                return View("Review", new ReviewModel(review));
            }

            return RedirectToAction("Index", "Home");
        }
    }
}